package gongqing.network.http;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import gongqing.network.beans.IrrBDDeviceDetail;
import com.xkh.xkh.MyApplication;
import managers.VolleySingleton;
import shuiyuqing.network.http.DownloadInfo;

/**
 * 获取基本设备信息
 * Created by 凯 on 2015/11/14.
 */
public class GetBasicDeviceInfo {

    Delegate delegate;

    Download download = new Download();

    Upload upload;
    public GetBasicDeviceInfo(Delegate delegate,Upload upload) {
        this.delegate = delegate;
        this.upload = upload;
    }

    // 获取下载信息
    public Download getDownload() {
        return download;
    }

    // 获取基本设备信息
    public void get()
    {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/guanqu/GetDeviceBasicInfo";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        afterGet(response);
                        if (delegate != null)
                        {
                            delegate.onResponse(response,GetBasicDeviceInfo.this);
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                {
                    delegate.onErrorResponse(error,GetBasicDeviceInfo.this);
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("data",JSON.toJSONString(upload));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    // 获取到响应信息
    private void afterGet(String response) {
        download = JSON.parseObject(response,Download.class);
        if (download.getStatus() == DownloadInfo.SUCCESS)
        {

        }else if (download.getStatus() == DownloadInfo.FAILED)
        {

        }
    }

    // 委托
    public static interface Delegate
    {
        public void onResponse(String response,GetBasicDeviceInfo getBasicDeviceInfo);
        public void onErrorResponse(VolleyError error,GetBasicDeviceInfo getBasicDeviceInfo);
    }


    // 下行数据
    public static class Download
    {
        int status;

        // 基本设备信息
        List<IrrBDDeviceDetail> details = new ArrayList<IrrBDDeviceDetail>();

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public List<IrrBDDeviceDetail> getDetails() {
            return details;
        }

        public void setDetails(List<IrrBDDeviceDetail> details) {
            this.details = details;
        }



    }

    // 上行数据
    public static class Upload
    {
        // 1,全部。2，按照类型。3，按照状态
        int flag;
        String devType;
        String devState;

        int from;

        public int getFlag() {
            return flag;
        }
        public void setFlag(int flag) {
            this.flag = flag;
        }
        public String getDevType() {
            return devType;
        }
        public void setDevType(String devType) {
            this.devType = devType;
        }
        public String getDevState() {
            return devState;
        }
        public void setDevState(String devState) {
            this.devState = devState;
        }
        public int getFrom() {
            return from;
        }
        public void setFrom(int from) {
            this.from = from;
        }
    }

}
